package com.zz.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zz.entity.Manager;
import com.zz.pojo.ResponseBo;
import com.zz.service.ManagerService;
import com.zz.util.KeyUtil;
import com.zz.util.MD5Utils;

@RestController
@RequestMapping("manager")
public class ManagerController {
	@Resource
	ManagerService managerService;

	//注册
	@RequestMapping("register")
	public Manager register(HttpServletRequest request) {
		String name=request.getParameter("inname");
		String pwd=request.getParameter("inpwd");
		String tel=request.getParameter("intel");
		String type=request.getParameter("intype");
		String password = MD5Utils.encrypt(name,pwd);
		Manager manager=new Manager();
		
			manager.setId(KeyUtil.genUniqueKey());
			manager.setName(name);
			manager.setPwd(password);
			manager.setTel(tel);
			manager.setType(type);
		return 	managerService.register(manager);
	}
	@RequestMapping("checkname")
	public Map checkname(HttpServletRequest request) {
		String name=request.getParameter("inname");
		Manager manager=managerService.findByname(name);
		boolean f=false;
		if(manager==null) {
			f=true;
		}else {
			f=false;
		}
		Map map=new HashMap();
		map.put("namecheck", f);
		return map;
	}
	
	
	//登录
	@RequestMapping("/login")
	public ResponseBo login(String uname, String upassword, Boolean rememberMe) {
		upassword = MD5Utils.encrypt(uname, upassword);//对密码进行加密，注册时的加密方法要和登录一样
		System.out.println("rememberMe------"+rememberMe+"  username--------"+uname+"  password------"+upassword);
		UsernamePasswordToken token = new UsernamePasswordToken(uname, upassword, rememberMe);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			return ResponseBo.ok();
		} catch (UnknownAccountException e) {
			return ResponseBo.error(e.getMessage());
		} catch (IncorrectCredentialsException e) {
			return ResponseBo.error(e.getMessage());
		} catch (LockedAccountException e) {
			return ResponseBo.error(e.getMessage());
		} catch (AuthenticationException e) {
			return ResponseBo.error("认证失败！");
		}
	}
	@RequestMapping("/")
	public String redirectIndex() {
		return "redirect:/ware";
	}

	@RequestMapping("/ware")
	public String index(Model model) {
		Manager manager = (Manager) SecurityUtils.getSubject().getPrincipal();//存在cookie中
		model.addAttribute("manager", manager);
		return "hk_warekc1.html";
	}
	
	@PostMapping("/getlogin")
	public Manager getLoginManager() {
		return (Manager) SecurityUtils.getSubject().getPrincipal();
	}
}
